package com.hichina.client;

import com.hichina.client.util.ResponseUtils;
import com.hichina.exception.HiChinaException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: zigang.wang
 * Date: 12-3-1
 * Time: 下午1:23
 * To change this template use File | Settings | File Templates.
 */
public class AccountList {
    private int totalsize;
    private List<Map<String, String>> mailList;
    public static final String MAP_KEY_UID = "uid";
    public static final String MAP_KEY_NAME = "name";
    public static final String MAP_KEY_MAIL = "mail";
    public static final String MAP_KEY_PHONE = "phone";
    public static final String MAP_KEY_WORK_PHONE = "work_phone";

    public AccountList() {
    }

    public AccountList(String jsonContent) throws HiChinaException {
        try {
            JSONObject json = ResponseUtils.checkResponse(jsonContent);
            setTotalsize(json.getInt("total"));
            JSONArray accounts = json.getJSONArray("accounts");

            List<Map<String, String>> mailList = new ArrayList<Map<String, String>>();
            for(int i=0; i<accounts.length(); i++){
                Map<String, String> map = new HashMap<String, String>();
                JSONObject item = accounts.getJSONObject(i);
                map.put(MAP_KEY_UID, item.getString(MAP_KEY_UID));
                map.put(MAP_KEY_NAME, item.getString(MAP_KEY_NAME));
                if(item.has(MAP_KEY_MAIL)){
                    map.put(MAP_KEY_MAIL, item.getString(MAP_KEY_MAIL));
                }
                if(item.has(MAP_KEY_PHONE)){
                    map.put(MAP_KEY_PHONE, item.getString(MAP_KEY_PHONE));
                }
                if(item.has(MAP_KEY_WORK_PHONE)){
                    map.put(MAP_KEY_WORK_PHONE,item.getString(MAP_KEY_WORK_PHONE));
                }
                mailList.add(map);
            }
            setMailList(mailList);
        } catch (JSONException e) {
            throw new HiChinaException(" can not conver to JSONObject");
        }
    }

    public int getTotalsize() {
        return totalsize;
    }

    public void setTotalsize(int totalsize) {
        this.totalsize = totalsize;
    }

    public List<Map<String, String>> getMailList() {
        return mailList;
    }

    public void setMailList(List<Map<String, String>> mailList) {
        this.mailList = mailList;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("列表：");
        for (Map<String, String> item : mailList) {
            sb.append("[");
            for(Map.Entry<String,String> entry : item.entrySet()){
                 sb.append(entry.getKey() + ":" + entry.getValue());
                sb.append(",");
            }
            sb.append("]<br/>");
        }
        return sb.toString();
    }
}
